﻿
//***********************************************************************
// * Module:   TbBstCaseReturnHandler
// * 作者:  曹荣芝
// * Modified: 2014/6/1
// * Purpose: Definition of the Class TbBstCaseReturnHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
using JWBST.CommonClass;

namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstCaseReturnHandler 的摘要说明
    /// </summary>
    public class TbBstCaseReturnHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {

                TbBstCaseReturn mTbBstCaseReturn = new TbBstCaseReturn();
                mTbBstCaseReturn.Id = Guid.NewGuid().ToString(); //主键
                mTbBstCaseReturn.Applyid = context.Request.Form["texApplyid"]; //外键，关联申请
                mTbBstCaseReturn.Casetype = context.Request.Form["texCasetype"]; //案件类型（01是刑事案件，02是行政案件）
                mTbBstCaseReturn.FileName = context.Request.Form["texFileName"]; //案件名称
                mTbBstCaseReturn.FileType = context.Request.Form["texFileType"]; //案别 罪名
                mTbBstCaseReturn.FileCompany = context.Request.Form["texFileCompany"]; //办案单位
                mTbBstCaseReturn.FilePolice = context.Request.Form["texFilePolice"]; //主办民警
                mTbBstCaseReturn.FileDate = context.Request.Form["texFileDate"]; //立案时间
                mTbBstCaseReturn.BailName = context.Request.Form["texBailName"]; //取保候审姓名
                mTbBstCaseReturn.BailDate = context.Request.Form["texBailDate"]; //取保候审时间
                mTbBstCaseReturn.MonitorName = context.Request.Form["texMonitorName"]; //监视居住姓名
                mTbBstCaseReturn.MonitorDate = context.Request.Form["texMonitorDate"]; //监视居住时间
                mTbBstCaseReturn.DetentionName = context.Request.Form["texDetentionName"]; //拘留姓名
                mTbBstCaseReturn.DetentionDate = context.Request.Form["texDetentionDate"]; //拘留拘留时间
                mTbBstCaseReturn.DetentionPlace = context.Request.Form["texDetentionPlace"]; //拘留在押地点
                mTbBstCaseReturn.ArrestName = context.Request.Form["texArrestName"]; //逮捕姓名
                mTbBstCaseReturn.ArrestDate = context.Request.Form["texArrestDate"]; //逮捕时间
                mTbBstCaseReturn.ArrestPlace = context.Request.Form["texArrestPlace"]; //羁押地点
                mTbBstCaseReturn.SueName = context.Request.Form["texSueName"]; //移送起诉姓名
                mTbBstCaseReturn.SueDate = context.Request.Form["texSueDate"]; //移送起诉时间
                mTbBstCaseReturn.SueProcuratorate = context.Request.Form["texSueProcuratorate"]; //移送起诉检察院
                mTbBstCaseReturn.AdAcceptName = context.Request.Form["texAdAcceptName"]; //行政案件受理案件名称
                mTbBstCaseReturn.AdAcceptType = context.Request.Form["texAdAcceptType"]; //行政案件受理案别 罪名
                mTbBstCaseReturn.AdAcceptCompany = context.Request.Form["texAdAcceptCompany"]; //行政案件受理办案单位
                mTbBstCaseReturn.AdAcceptPolice = context.Request.Form["texAdAcceptPolice"]; //行政案件受理主办民警
                mTbBstCaseReturn.AdAcceptDate = context.Request.Form["texAdAcceptDate"]; //行政案件受理立案时间
                mTbBstCaseReturn.AdPenaltyName = context.Request.Form["texAdPenaltyName"]; //行政处罚姓名
                mTbBstCaseReturn.AdPenaltyResult = context.Request.Form["texAdPenaltyResult"]; //行政处罚结果

                mResultModel = TbBstCaseReturnBLL.Insert(mTbBstCaseReturn, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstCaseReturn mTbBstCaseReturn = new TbBstCaseReturn();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstCaseReturnBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstCaseReturn mTbBstCaseReturn = new TbBstCaseReturn();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstCaseReturn = TbBstCaseReturnBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstCaseReturn != null)
                {
                    mTbBstCaseReturn.Applyid = context.Request.Form["texApplyid"]; //外键，关联申请
                    mTbBstCaseReturn.Casetype = context.Request.Form["texCasetype"]; //案件类型（01是刑事案件，02是行政案件）
                    mTbBstCaseReturn.FileName = context.Request.Form["texFileName"]; //案件名称
                    mTbBstCaseReturn.FileType = context.Request.Form["texFileType"]; //案别 罪名
                    mTbBstCaseReturn.FileCompany = context.Request.Form["texFileCompany"]; //办案单位
                    mTbBstCaseReturn.FilePolice = context.Request.Form["texFilePolice"]; //主办民警
                    mTbBstCaseReturn.FileDate = context.Request.Form["texFileDate"]; //立案时间
                    mTbBstCaseReturn.BailName = context.Request.Form["texBailName"]; //取保候审姓名
                    mTbBstCaseReturn.BailDate = context.Request.Form["texBailDate"]; //取保候审时间
                    mTbBstCaseReturn.MonitorName = context.Request.Form["texMonitorName"]; //监视居住姓名
                    mTbBstCaseReturn.MonitorDate = context.Request.Form["texMonitorDate"]; //监视居住时间
                    mTbBstCaseReturn.DetentionName = context.Request.Form["texDetentionName"]; //拘留姓名
                    mTbBstCaseReturn.DetentionDate = context.Request.Form["texDetentionDate"]; //拘留拘留时间
                    mTbBstCaseReturn.DetentionPlace = context.Request.Form["texDetentionPlace"]; //拘留在押地点
                    mTbBstCaseReturn.ArrestName = context.Request.Form["texArrestName"]; //逮捕姓名
                    mTbBstCaseReturn.ArrestDate = context.Request.Form["texArrestDate"]; //逮捕时间
                    mTbBstCaseReturn.ArrestPlace = context.Request.Form["texArrestPlace"]; //羁押地点
                    mTbBstCaseReturn.SueName = context.Request.Form["texSueName"]; //移送起诉姓名
                    mTbBstCaseReturn.SueDate = context.Request.Form["texSueDate"]; //移送起诉时间
                    mTbBstCaseReturn.SueProcuratorate = context.Request.Form["texSueProcuratorate"]; //移送起诉检察院
                    mTbBstCaseReturn.AdAcceptName = context.Request.Form["texAdAcceptName"]; //行政案件受理案件名称
                    mTbBstCaseReturn.AdAcceptType = context.Request.Form["texAdAcceptType"]; //行政案件受理案别 罪名
                    mTbBstCaseReturn.AdAcceptCompany = context.Request.Form["texAdAcceptCompany"]; //行政案件受理办案单位
                    mTbBstCaseReturn.AdAcceptPolice = context.Request.Form["texAdAcceptPolice"]; //行政案件受理主办民警
                    mTbBstCaseReturn.AdAcceptDate = context.Request.Form["texAdAcceptDate"]; //行政案件受理立案时间
                    mTbBstCaseReturn.AdPenaltyName = context.Request.Form["texAdPenaltyName"]; //行政处罚姓名
                    mTbBstCaseReturn.AdPenaltyResult = context.Request.Form["texAdPenaltyResult"]; //行政处罚结果

                    mResultModel = TbBstCaseReturnBLL.Update(mTbBstCaseReturn, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstCaseReturn mTbBstCaseReturn = new TbBstCaseReturn(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstCaseReturn = TbBstCaseReturnBLL.Load(paramKey); //根据键值获取管理员
                    //if (mTbBstCaseReturn.Casetype == "01")
                    //{
                    //    //mTbBstCaseReturn.CasetypeFlag = "1";
                    //    mTbBstCaseReturn.Casetype = "刑事案件";
                    //}
                    //else
                    //{
                    //    //mTbBstCaseReturn.CasetypeFlag = "2";
                    //    mTbBstCaseReturn.Casetype = "行政案件";
                    //}
                    mTbBstCaseReturn.AdAcceptCompany = mTbBstCaseReturn.AdAcceptCompany.FilteNullReturnEmpty();
                    mTbBstCaseReturn.AdAcceptDate = mTbBstCaseReturn.AdAcceptDate.FilteNullReturnEmpty();
                    mTbBstCaseReturn.AdAcceptName = mTbBstCaseReturn.AdAcceptName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.AdAcceptPolice = mTbBstCaseReturn.AdAcceptPolice.FilteNullReturnEmpty();
                    mTbBstCaseReturn.AdAcceptType = mTbBstCaseReturn.AdAcceptType.FilteNullReturnEmpty();
                    mTbBstCaseReturn.AdPenaltyName = mTbBstCaseReturn.AdPenaltyName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.AdPenaltyResult = mTbBstCaseReturn.AdPenaltyResult.FilteNullReturnEmpty();
                    mTbBstCaseReturn.ArrestDate = mTbBstCaseReturn.ArrestDate.FilteNullReturnEmpty();
                    mTbBstCaseReturn.ArrestName = mTbBstCaseReturn.ArrestName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.ArrestPlace = mTbBstCaseReturn.ArrestPlace.FilteNullReturnEmpty();
                    mTbBstCaseReturn.BailDate = mTbBstCaseReturn.BailDate.FilteNullReturnEmpty();
                    mTbBstCaseReturn.BailName = mTbBstCaseReturn.BailName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.Casetype = mTbBstCaseReturn.Casetype.FilteNullReturnEmpty();
                    //mTbBstCaseReturn.CasetypeFlag = mTbBstCaseReturn.CasetypeFlag.FilteNullReturnEmpty();
                    mTbBstCaseReturn.DetentionDate = mTbBstCaseReturn.DetentionDate.FilteNullReturnEmpty();
                    mTbBstCaseReturn.DetentionName = mTbBstCaseReturn.DetentionName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.DetentionPlace = mTbBstCaseReturn.DetentionPlace.FilteNullReturnEmpty();
                    mTbBstCaseReturn.FileCompany = mTbBstCaseReturn.FileCompany.FilteNullReturnEmpty();
                    mTbBstCaseReturn.FileDate = mTbBstCaseReturn.FileDate.FilteNullReturnEmpty();
                    mTbBstCaseReturn.FileName = mTbBstCaseReturn.FileName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.FilePolice = mTbBstCaseReturn.FilePolice.FilteNullReturnEmpty();
                    mTbBstCaseReturn.FileType = mTbBstCaseReturn.FileType.FilteNullReturnEmpty();
                    mTbBstCaseReturn.MonitorDate = mTbBstCaseReturn.MonitorDate.FilteNullReturnEmpty();
                    mTbBstCaseReturn.MonitorName = mTbBstCaseReturn.MonitorName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.SueDate = mTbBstCaseReturn.SueDate.FilteNullReturnEmpty();
                    mTbBstCaseReturn.SueName = mTbBstCaseReturn.SueName.FilteNullReturnEmpty();
                    mTbBstCaseReturn.SueProcuratorate = mTbBstCaseReturn.SueProcuratorate.FilteNullReturnEmpty();
                    if (mTbBstCaseReturn != null)
                    {
                        json = JsonConvert.SerializeObject(mTbBstCaseReturn);
                    }

                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string Name = context.Request.Form["texName"];         //用户名
            string ZpCode = context.Request.Form["selStatus"];    //操作类型
            //DateTime begintime = new DateTime();
            //DateTime endtime = new DateTime();
            //if (!string.IsNullOrEmpty(context.Request.Form["texTimeStart"]))
            //{
            //    begintime = Convert.ToDateTime(context.Request.Form["texTimeStart"]);
            //}
            //if (!string.IsNullOrEmpty(context.Request.Form["texTimeEnd"]))
            //{
            //    endtime = Convert.ToDateTime(context.Request.Form["texTimeEnd"]);
            //}
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstCaseReturn = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(Name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstCaseReturnConst.Columns.FileName, '%' + Name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(ZpCode))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstCaseReturnConst.Columns.Casetype, ZpCode), SqlLogicOper.AND_LOGIC);
            }
            //if (begintime.ToString() != "0001/1/1 0:00:00")
            //{
            //    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstCaseReturnConst.Columns.FileDate, begintime), SqlLogicOper.AND_LOGIC);
            //}
            //if (endtime.ToString() != "0001/1/1 0:00:00")
            //{
            //    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstCaseReturnConst.Columns.FileDate, endtime), SqlLogicOper.AND_LOGIC);
            //}
            dtTbBstCaseReturn = Q.GetRecordByPage("*", sqllist.ToSql(), "FILE_DATE", intPageSize, intPageIndex, TbBstCaseReturnConst.TableName, TbBstCaseReturnConst.FieldKey, "DESC").Tables[0];
            if (dtTbBstCaseReturn != null && dtTbBstCaseReturn.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstCaseReturn.Rows.Count; i++)
                {
                    if (dtTbBstCaseReturn.Rows[i]["CASETYPE"].ToString() == "01")
                    {
                        dtTbBstCaseReturn.Rows[i]["CASETYPE"] = "刑事案件";
                        dtTbBstCaseReturn.Rows[i]["FILE_NAME"] = dtTbBstCaseReturn.Rows[i]["FILE_NAME"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_TYPE"] = dtTbBstCaseReturn.Rows[i]["FILE_TYPE"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_COMPANY"] = dtTbBstCaseReturn.Rows[i]["FILE_COMPANY"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_POLICE"] = dtTbBstCaseReturn.Rows[i]["FILE_POLICE"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_DATE"] = dtTbBstCaseReturn.Rows[i]["FILE_DATE"].ToString().FilteNullReturnEmpty();
                    }
                    else
                    {
                        dtTbBstCaseReturn.Rows[i]["CASETYPE"] = "行政案件";
                        dtTbBstCaseReturn.Rows[i]["FILE_NAME"] = dtTbBstCaseReturn.Rows[i]["AD_ACCEPT_NAME"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_TYPE"] = dtTbBstCaseReturn.Rows[i]["AD_ACCEPT_TYPE"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_COMPANY"] = dtTbBstCaseReturn.Rows[i]["AD_ACCEPT_COMPANY"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_POLICE"] = dtTbBstCaseReturn.Rows[i]["AD_ACCEPT_POLICE"].ToString().FilteNullReturnEmpty();
                        dtTbBstCaseReturn.Rows[i]["FILE_DATE"] = dtTbBstCaseReturn.Rows[i]["AD_ACCEPT_DATE"].ToString().FilteNullReturnEmpty();
                    }
                }
                for (int i = 0; i < dtTbBstCaseReturn.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstCaseReturn.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstCaseReturn;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


